package Scene;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.buttons.MTImageButton;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.globalProcessors.CursorTracer;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.sceneManagement.transition.BlendTransition;
import org.mt4j.sceneManagement.transition.FadeTransition;
import org.mt4j.util.MT4jSettings;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.opengl.GLFBO;

import processing.core.PImage;

public class EndGameScene extends AbstractScene {

	private int winningPlayer;
	private MTApplication app;
	private String winText;
	
	public EndGameScene(MTApplication mtApplication, String name, int _winningPlayer, long _score) {
		super(mtApplication, name);
		
		app = mtApplication;
		
		//Set the background color
		this.setClearColor(new MTColor(146, 150, 188, 255));
		
		this.registerGlobalInputProcessor(new CursorTracer(app, this));
	
		winningPlayer = _winningPlayer;
		if(winningPlayer == 1){
			winText = "Spieler BLAU hat gewonnen!\n\tmit "+_score+" Punkten.";
		} else if(winningPlayer == 2){
			winText = "Spieler ROT hat gewonnen!\n\tmit "+_score+" Punkten.";
		} else {
			winText = "Unentschieden!\n\t"+_score+" Punkte";
		}
		
		PImage background = mtApplication.loadImage("images/endgame.png");
		MTRectangle backgroundRec = new MTRectangle(background, app);
		backgroundRec.setPositionGlobal(new Vector3D(app.width/2f,app.height/2f));
		backgroundRec.removeAllGestureEventListeners();
		getCanvas().addChild(backgroundRec);
		
		MTTextArea textField = new MTTextArea(app, FontManager.getInstance().createFont(app, "arial.ttf", 
				50, new MTColor(255, 255, 255, 255), new MTColor(255, 255, 255, 255))); 
		textField.setNoFill(true);
		textField.setNoStroke(true);
		textField.setText(winText);
		getCanvas().addChild(textField);
		textField.setPositionGlobal(new Vector3D(app.width/2f, app.height/2+75));
		
		PImage arrow = mtApplication.loadImage("images/mainmenu.png");
		MTImageButton nextSceneButton = new MTImageButton(arrow, mtApplication);
		nextSceneButton.setNoStroke(true);
		if (MT4jSettings.getInstance().isOpenGlMode())
			nextSceneButton.setUseDirectGL(true);
		nextSceneButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				switch (ae.getID()) {
				case TapEvent.BUTTON_CLICKED:
					app.changeScene(app.getScene("Finger Break!"));
					break;
				default:
					break;
				}
			}
		});
		getCanvas().addChild(nextSceneButton);
		nextSceneButton.setPositionGlobal(new Vector3D(app.width/2, app.height/2+200));
		
		//Set a scene transition - Flip transition only available using opengl supporting the FBO extenstion
		if (MT4jSettings.getInstance().isOpenGlMode() && GLFBO.isSupported(app))
			this.setTransition(new BlendTransition(app, 700));
		else {
			this.setTransition(new FadeTransition(mtApplication, 1700));
		}
		
		
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void shutDown() {
		// TODO Auto-generated method stub
		
	}
}

